import Vue from 'vue'
import App from './App.vue'
import store from './store'
import router from './router'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import 'normalize.css/normalize.css'
import 'font-awesome/css/font-awesome.min.css'
import {getToken} from './utils/cookie'
import '@/assets/styles/index.scss' // global css

Vue.config.productionTip = false;

NProgress.configure({showSpinner: false});

const whiteList = ['/index', '/login', '/auth-redirect', '/bind', '/register'];
router.beforeEach((to, from, next) => {
  console.log(to.path);
  NProgress.start();
  if (getToken()) {
    if (to.path === '/login') {
      NProgress.done();
      next({path: '/'});
    } else {
      next()
    }
  } else {
    if (whiteList.indexOf(to.path) !== -1) {
      console.log("白名单放行");
      store.dispatch('generateRoutes').then(accessRoutes => {
        // console.log(this.$store.state.count);
        router.addRoutes(accessRoutes);
        next()
      });
    } else {
      NProgress.done();
      next('/login');
    }
  }
});

router.afterEach((to) => {
  NProgress.done();
  window.scrollTo(0, 0);
  window.document.title = to.meta.title || '前后端分离开发平台';
});

Vue.use(ElementUI);

new Vue({
  router,
  store,
  render: h => h(App),
}).$mount('#app');
